﻿// StockTicker 
// - Microsoft StreamInsight application example
// - Contains input adapter for reading quotes from Yahoo Finance
// (C) Johan Åhlén, 2009

using System;
using System.Threading;
using Microsoft.ComplexEventProcessing;
using Microsoft.ComplexEventProcessing.Linq;
using Microsoft.ComplexEventProcessing.Diagnostics;

namespace AdvantIQ.StockTicker
{
    /// <summary>
    /// Main class.
    /// </summary>
    public class StockTicker
    {
        public const int Timeout = 10000;
        public const int Interval = 2000;
        public const int NumberOfReading = 100;
        public const string Symbol1 = "^GSPC";
        public const string Name1 = "S&P 500";
        public const string Symbol2 = "^DJI";
        public const string Name2 = "Dow Jones Industrial";

        /// <summary>
        /// Starting point.
        /// </summary>
        public static void Main()
        {
            var server = Server.Create("SI");
            var application = server.CreateApplication("StockTicker");

            // Configuration for input 1
            var input1Config = new StockTickerInputConfig
            {
                Timeout = StockTicker.Timeout,
                StockName = StockTicker.Name1,
                Interval = StockTicker.Interval,
                NumberOfReadings = StockTicker.NumberOfReading,
                StockSymbol = StockTicker.Symbol1
            };

            // Configuration for input 2
            var input2Config = new StockTickerInputConfig
            {
                Timeout = StockTicker.Timeout,
                StockName = StockTicker.Name2,
                Interval = StockTicker.Interval,
                NumberOfReadings = StockTicker.NumberOfReading,
                StockSymbol = StockTicker.Symbol2
            };

            // Instantiate input adapters
            var input1Stream = CepStream<StockPayload>.Create("StockTickerInput1", typeof(StockTickerInputFactory), input1Config, EventShape.Point);
            var input2Stream = CepStream<StockPayload>.Create("StockTickerInput2", typeof(StockTickerInputFactory), input2Config, EventShape.Point);

            // Configure output adapter
            var outputConfig = new StockTickerOutputConfig { AdapterStopSignal = "StockTickerWaitHandle" };

            // Join input adapters with a simple LINQ query
            var combinedInputStream = (from e in input1Stream
                                       select e).Union
                                      (from e in input2Stream
                                       select e);

            // Connect input adapters with output adapter
            var query = combinedInputStream.ToQuery(application, "StockTicker", "...", typeof(StockTickerOutputFactory), outputConfig, EventShape.Point, StreamEventOrder.ChainOrdered);

            // Instantiate semaphor for stop signal
            var adapterStopSignal = new EventWaitHandle(false, 
                EventResetMode.ManualReset, outputConfig.AdapterStopSignal);

            // Run the query
            query.Start();
            adapterStopSignal.WaitOne();
            query.Stop();

            application.Delete();
            server.Dispose();
        }
    }
}
